/*******************************************************************************
 * Copyright (c) 2012, 2013 XML-Print Project.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     XML-Print Project - initial API and implementation
 ******************************************************************************/
package info.xmlprint.action;

import info.xmlprint.core.project.PrintProject;
import info.xmlprint.run.Configuration;
import info.xmlprint.run.RuntimeUtil;
import info.xmlprint.run.XML2Foplus;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;

public class PDFPreview extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		RuntimeUtil rt = RuntimeUtil.getInstance();
		PrintProject pProject = rt.getCurrentPrintProject();
		if (pProject == null) {
			return null;
		}
		IProject project = pProject.getProject();
		if (project != null) {
			// fo erzeugen 
			XML2Foplus pdfOut = new XML2Foplus(rt.getContentText(pProject));
			pdfOut.setXslSour(pProject.getMappingGroup().getXslDoc());

			if (Configuration.getInstance().getEngineName()
					.equalsIgnoreCase("Fop")) {//$NON-NLS-1$
				pdfOut.preview(0.4, null, true);
			} else {
				// fo to pdf transformieren
				CmdToPDF ctp = new CmdToPDF();
				String foPath = pdfOut.getResultFoFile();
				rt.refreshFo(pProject, foPath);
				if (!ctp.isStop() && foPath != null) {
					ctp.transform();
					ctp.pdfDisplay();
				}
			}
		}
		return null;
	}
}
